package com.daikun.common.model;

import com.daikun.common.enums.BaseResultEnum;
import com.daikun.common.iface.BaseResultIface;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
 * 统一 web 返回对象格式
 * @author earl created 2025-07-03
 * @version init 0.0.1
 */
@Getter
@Setter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BaseResult<T> extends BaseModel {

    private final int code;
    private final String msg;
    private final T data;

    public static <T> BaseResult<T> ok() {
        return ok(null);
    }

    public static <T> BaseResult<T> ok(T data) {
        return ok(BaseResultEnum.SUCCESS, data);
    }

    public static <T> BaseResult<T> ok(BaseResultIface item, T data) {
        return new BaseResult<T>(item.getCode(), item.getMsg(), data);
    }

    public static <T> BaseResult<T> fail(BaseResultIface item) {
        return fail(item, item.getMsg());
    }

    public static <T> BaseResult<T> fail(BaseResultIface item, String msg) {
        return fail(item, msg, null);
    }

    public static <T> BaseResult<T> fail(BaseResultIface item, String msg, T data) {
        return new BaseResult<T>(item.getCode(), msg, data);
    }

    public static <T> BaseResult<T> fail(BaseResultIface item, T data) {
        return new BaseResult<T>(item.getCode(), item.getMsg(), data);
    }

}
